﻿using System;
using System.Linq;
using FluentValidation;
using LINQPad.OpenAccess.Helpers;

namespace LINQPad.OpenAccess.UI.ViewModel.Validators
{
    /// <summary>
    /// Validator for <see cref="DriverSettingsViewModel"/>
    /// </summary>
    public class DriverSettingsViewModelValidator : AbstractValidator<DriverSettingsViewModel>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DriverSettingsViewModelValidator" /> class.
        /// </summary>
        /// <param name="basicSettingsValidator">The basic settings validator.</param>
        /// <param name="backendSettingsValidator">The backend settings validator.</param>
        /// <param name="connectionSettingHostValidator">The connection setting host validator.</param>
        public DriverSettingsViewModelValidator( IValidator<BasicSettingsViewModel> basicSettingsValidator
                                                    , IValidator<BackendSettingsViewModel> backendSettingsValidator
                                                    , IValidator<ConnectionSettingsHostViewModel> connectionSettingHostValidator )
        {
            Guard.ArgumentNotNull( basicSettingsValidator, "basicSettingsValidator" );
            Guard.ArgumentNotNull( backendSettingsValidator, "backendSettingsValidator" );

            this.RuleFor( d => d.BasicSettings )
                .SetValidator( basicSettingsValidator )
                .WithMessage( "basic settings are invalid !" );

            this.RuleFor( d => d.BackendSettings )
                .SetValidator( backendSettingsValidator )
                .WithMessage( "backend settings are invalid !" )
                .Unless( d => d.BasicSettings.UseDefaultConstructor );

            this.RuleFor( d => d.ConnectionSettingsHost )
                .SetValidator( connectionSettingHostValidator )
                .WithMessage( "connection settings are invalid !" )
                .Unless( d => d.BasicSettings.UseDefaultConstructor );
        }
    }
}
